package fantasyday.wowzolo.web.common;

import fantasyday.wowzolo.web.account.EditAccountPage;
import fantasyday.wowzolo.web.account.SignonPage;
import fantasyday.wowzolo.web.cart.CartPage;
import fantasyday.wowzolo.web.catalog.IndexPage;
import fantasyday.wowzolo.domain.Account;
import fantasyday.wowzolo.service.AccountService;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.PageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.spring.injection.annot.SpringBean;

public abstract class BasePage extends WebPage {
    	@SpringBean
	private AccountService accountService;
	@SuppressWarnings("serial")
	public BasePage() {
		add(new BookmarkablePageLink("index", IndexPage.class));
		
		add(new BookmarkablePageLink("viewCart", CartPage.class));
		
		add(new BookmarkablePageLink("signon", SignonPage.class) {	
			@Override
			public boolean isVisible() {
				return !isSignon();
			}
		});
		
		add(new PageLink("signoff", IndexPage.class) {	
			@Override
			public void onClick() {
				getJpetstoreSession().setSignonedAccount(null);
				setRedirect(true);
				super.onClick();
			}
			@Override
			public boolean isVisible() {
				return isSignon();
			}
		});
		
		add(new BookmarkablePageLink("editAccount", EditAccountPage.class) {	
			@Override
			public boolean isVisible() {
				return isSignon();
			}
		});
		//Account account = accountService.getAccount("j2ee");
//        String username = account.getUsername();
        String username = "dummy";
        add(new Label("username",username));
        
		Form form = new Form("searchProducts") {
			@Override
			protected void onSubmit() {
				System.out.println("search!");
			}
		};
		
		RequiredTextField keyword = new RequiredTextField("keyword");
		
		form.add(keyword);
		
		add(form);

		
		add(new ViewCategoryPageLink("viewCategoryFish", "FISH"));

		add(new ViewCategoryPageLink("viewCategoryDog", "DOG"));

		add(new ViewCategoryPageLink("viewCategoryReptile", "REPTILE"));

		add(new ViewCategoryPageLink("viewCategoryCat", "CAT"));

		add(new ViewCategoryPageLink("viewCategoryBird", "BIRD"));
		
		add(new FeedbackPanel("feedback"));
	}
	
	public WowzoloSession getJpetstoreSession() {
		return (WowzoloSession) super.getSession();
	}
	
	public boolean isSignon() {
		return getJpetstoreSession().isSignon();
	}

    public Account getSignonedAccount() {
        return getJpetstoreSession().getSignonedAccount();
    }

    public void setSignonedAccount(Account account) {
        getJpetstoreSession().setSignonedAccount(account);
    }

    public void error(String message) {
        getJpetstoreSession().error(message);
    }
}
